import 'dart:ui';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:provider/provider.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBNavigator.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBWeChatPopPage/YBCameraSelectPopPage.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBWeChatPopPage/YBImageEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBImageStyle/YBImageStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/yb_chinese_text/yb_chinese_text.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBBaseModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineUserInfoModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineProvider/YBMineUserProvider.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBQiYePersonPage/YBNumberManagerProvider/YBQiYeAndPersonManagerEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBQiYePersonPage/YBNumberManagerProvider/YBYBImageProvider.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBAPi/YBAPi.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBNetWorkModule/YBBaseMineNetWork/YBBaseMineNetWork.dart';

class YBQiYeAuthMainpage extends StatefulWidget {
  @override
  _YBQiYeAuthMainpageState createState() => _YBQiYeAuthMainpageState();
}

class _YBQiYeAuthMainpageState extends State<YBQiYeAuthMainpage> {

  String _nickNameStr;
  String _cercode;
  String _legalPerson;
  String _imageStr;



  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    qiYeAndPersonManagerEventBus.on<YBQiYeAndPersonManagerEventBus>().listen((event) {
      if (event.title.contains('公司名称')){
        setState(() {
          _nickNameStr = event.content;
        });
      }else if (event.title.contains('统一社会信用代码')){

        setState(() {
          _cercode = event.content;
        });
      }else if(event.title.contains('法定代表人')){
        print("----${event.content}--");
        setState(() {
          _legalPerson = event.content;
        });
      }
    });

  }

  @override
  void dispose() {
    // TODO: implement dispose
    qiYeAndPersonManagerEventBus.destroy();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text(YBChineseText.mineQiYeAuthTitle,style: YBTextStyle.TextStyle30313320Medium),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Expanded(
              child: ChangeNotifierProvider(
                create: (context) => YBMineUserProvider()..getUserInfoData(),
                child: Consumer<YBMineUserProvider>(
                    builder:(context,dataUserInfo,child){

                      String _nickNameContanerStr;
                      String _cercodeContaienrStr;
                      String _legalPersonContainerStr;
                      String _imageContainerStr;

                      _nickNameContanerStr = _nickNameStr??dataUserInfo.mineUserInfoModel.customerName;
                      _cercodeContaienrStr = _cercode??dataUserInfo.mineUserInfoModel.creditCode;
                      _legalPersonContainerStr = _legalPerson??dataUserInfo.mineUserInfoModel.legalPerson;
                      _imageContainerStr = _imageStr??dataUserInfo.mineUserInfoModel.certCredUrl;

                      return SingleChildScrollView(
                        child: Column(
                          children: [
                            Container(
                              color: YBColor.white,
                              child: Padding(
                                padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                                child: Column(
                                  children: [
                                    Container(
                                      child: InkWell(
                                        child: _getLoadInfo(YBChineseText.mineCompanyNameTitle, _nickNameContanerStr??""),
                                        onTap: (){
                                          YBApplication.router.navigateTo(context, '/mine/mineQiYePersonPage/mineQuanCengHomePage/mineQiYePersonEditingPage?titleHeader=${Uri.encodeComponent("公司名称")}&&contentTitle=${Uri.encodeComponent(_nickNameContanerStr)}');
                                        },
                                      ),
                                    ),
                                    Container(
                                      child: InkWell(
                                        child: _getLoadInfo(YBChineseText.mineTongYiSheHuiXinYongDaiMaTitle, _cercodeContaienrStr??""),
                                        onTap: (){
                                          YBApplication.router.navigateTo(context, '/mine/mineQiYePersonPage/mineQuanCengHomePage/mineQiYePersonEditingPage?titleHeader=${Uri.encodeComponent("统一社会信用代码")}&&contentTitle=${Uri.encodeComponent(_cercodeContaienrStr)}');
                                        },
                                      ),
                                    ),
                                    Container(
                                      child: InkWell(
                                        child: _getLoadInfo(YBChineseText.mineFaDingDaiBiaoRenTitle, _legalPersonContainerStr??""),
                                        onTap: (){
                                          YBApplication.router.navigateTo(context, '/mine/mineQiYePersonPage/mineQuanCengHomePage/mineQiYePersonEditingPage?titleHeader=${Uri.encodeComponent("法定代表人")}&&contentTitle=${Uri.encodeComponent(_legalPersonContainerStr)}');
                                        },
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            Container(
                              height: YBTextStyle.adapterSize10,
                            ),
                            Container(
                              color: YBColor.white,
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: [
                                  Container(
                                    height: YBTextStyle.adapterSize10,
                                  ),
                                  Container(
                                    child: Padding(
                                      padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right:YBTextStyle.adapterSize20),
                                      child: Text(
                                        YBChineseText.mineYingYeZhiZhaoTitle,
                                        style: YBTextStyle.TextStyle2F303314,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              height: YBTextStyle.adapterSize10,
                              color: YBColor.white,
                            ),
                            Container(
                              height: YBTextStyle.adapterSize100,
                              color: YBColor.white,
                              child: Padding(
                                padding:EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Expanded(
                                      child: Image.asset(YBImageStyle.getImagePath('yingyezhizhao_imageView')),
                                    ),
                                    Container(
                                      width: YBTextStyle.adapterSize20,
                                    ),
                                    //CachedNetworkImage(imageUrl:'${YBApi.Base_image_URL}/$_imageStr',errorWidget: (context,url,eror)=>Image.asset(YBImageStyle.getImagePath("authjiahao")))
                                    Expanded(
                                      child: Container(
                                        height: YBTextStyle.adapterSize80,
                                        child: InkWell(
                                          child: Stack(
                                            children: [
                                              Container(
                                                child: ChangeNotifierProvider(create:(context)=>YBImageProvider()..getimageStr(_imageContainerStr),
                                                  child: Consumer<YBImageProvider>(builder:(context,imageInfo,child){
                                                    imageStrEventBus.on<YBImageEventBus>().listen((event) {
                                                      print('-----${event.imageStr}-');
                                                      setState(() {
                                                        _imageStr = event.imageStr;
                                                      });
                                                      imageInfo.getimageStr(event.imageStr);
                                                    });
                                                    return Center(
                                                      child: CachedNetworkImage(imageUrl:'${YBApi.Base_image_URL}/${imageInfo.imageStr}',fit:BoxFit.fill,errorWidget: (context,url,eror)=>Image.asset(YBImageStyle.getImagePath("authjiahao"))),
                                                    );
                                                  }),
                                                ),
                                              ),
                                            ],
                                          ),
                                          onTap: () async {
                                            YBCameraSelectPopPage.getCameraSelectPop(context);
                                          },
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            Container(
                              color: YBColor.white,
                              height: YBTextStyle.adapterSize10,
                            ),
                            Container(
                              color: YBColor.white,
                              child:Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Expanded(
                                    child: Center(
                                      child: Text(
                                        YBChineseText.mineYingYeZhiZhaoShiLiMianTitle,
                                        style: YBTextStyle.TextStyleA9AEB812,
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    child: Center(
                                      child: Text(
                                        YBChineseText.mineYingYeZhiZhaoShiLiMiaoUplpadTitle,
                                        style: YBTextStyle.TextStyleA9AEB812,
                                      ),
                                    ),
                                  ),

                                ],
                              ),
                            ),
                          ],
                        ),
                      );
                    }
                ),
              ),
            ),
            Container(
              height: YBTextStyle.adapterSize45,
              child: Padding(
                padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right: YBTextStyle.adapterSize20),
                child: RawMaterialButton(
                  child: Text(YBChineseText.mineTiJiAuthTitle,style: YBTextStyle.TextStyleFFFFFF14Sembold,),
                  fillColor: YBColor.commonThemeColor,
                  shape:RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(YBTextStyle.adapterSize6),
                  ),
                  onPressed: () async {
                    _editingTiJiao(_nickNameStr, _cercode, _legalPerson, _imageStr);
                  },
                ),
              ),
            ),
            Container(
              height: YBTextStyle.adapterSize20,
            ),
          ],
        ),
      ),
    );
  }
  Widget _getLoadInfo(String title, String bodyTitle){
    return Container(
      height: YBTextStyle.adapterSize58,
      child: Row(
        children: [
          Container(
            child: Text(
              title,
              style: YBTextStyle.TextStyle2F303314,
            ),
          ),
          Expanded(
            child: Container(
              alignment: Alignment.centerRight,
              child: Text(
                bodyTitle,
                style: YBTextStyle.TextStyle2F303314,
              ),
            ),
          ),
          Container(
            width: YBTextStyle.adapterSize5,
          ),
          Container(
            child: Image.asset(YBImageStyle.getImagePath('findIconArrowRight')),
          ),
        ],
      ),
    );
  }

  void _editingTiJiao(String certLinkName, String cerdCertCode,String legalPerson,String certCredUrl){
    Map<String, dynamic> editingMap = Map();
    editingMap['sysCode'] = "xg_youbang";
    editingMap['certStatus'] = -1;
    editingMap['certLinkName'] = certLinkName;
    editingMap['legalPerson'] = legalPerson;
    editingMap['creditCode'] = cerdCertCode;
    editingMap['certCredUrl'] = certCredUrl;

    YBMineNetWork.mineQiYeAndPersonEditingData(editingMap,success:(data){
      print('---$data-');
      YBBaseModel baseModel = YBBaseModel.fromJson(data);
      if (baseModel.code == 0) {
        EasyLoading.showInfo("提交成功");
        YBNavigatorUtil.goBack(context);
      }
    },fail: (error){
      print("---$error--");
    });
  }

}


